﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    Mobil1Entity ctx = new Mobil1Entity();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlCate.SelectedIndex = 0;
            //int cateId = Convert.ToInt32(ddlCate.SelectedValue);
            FillGridView(1);
        }
        gvMobil.RowEditing += gvMobil_RowEditing;
        gvMobil.RowCancelingEdit += gvMobil_RowCancelingEdit;
        gvMobil.RowUpdating += gvMobil_RowUpdating;
        gvMobil.RowDeleting += gvMobil_RowDeleting;
    }

    void gvMobil_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int mId = Convert.ToInt32(e.Keys[0]);
        Mobil curMobil = ctx.Mobils.Where(m => m.MobilId == mId).SingleOrDefault<Mobil>();
        ctx.Mobils.Remove(curMobil);
        ctx.SaveChanges();
        int cateId = Convert.ToInt32(ddlCate.SelectedValue);
        FillGridView(cateId);
    }

    void gvMobil_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int mId = Convert.ToInt32(e.NewValues[0]);
        string mName = e.NewValues[1].ToString();
        int mPrice = Convert.ToInt32(e.NewValues[2]);
        //TextBox tbId = gvMobil.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox;
        //int mId1 = Convert.ToInt32(tbId.Text);
        //var curM = from m in ctx.Mobils
        //           where m.MobilId == mId
        //           select m;
        //Mobil mm = curM.SingleOrDefault<Mobil>();
        Mobil curMobil = ctx.Mobils.Where(m => m.MobilId == mId).SingleOrDefault<Mobil>();
        curMobil.Name = mName;
        curMobil.Price = mPrice;
        ctx.SaveChanges();
        gvMobil.EditIndex = -1;
        int cateId = Convert.ToInt32(ddlCate.SelectedValue);
        FillGridView(cateId);
    }

    void gvMobil_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvMobil.EditIndex = -1;
        int cateId = Convert.ToInt32(ddlCate.SelectedValue);
        FillGridView(cateId);
    }

    void gvMobil_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvMobil.EditIndex = e.NewEditIndex;
        int cateId = Convert.ToInt32(ddlCate.SelectedValue);
        FillGridView(cateId);
        TextBox tbId = gvMobil.Rows[e.NewEditIndex].Cells[0].Controls[0] as TextBox;
        tbId.Enabled = false;
    }
    protected void ddlCate_SelectedIndexChanged(object sender, EventArgs e)
    {
        int cateId = Convert.ToInt32(ddlCate.SelectedValue);
        FillGridView(cateId);
    }
    private void FillGridView(int cateId)
    {
        var mobils = from m in ctx.Mobils
                     where m.Category.CateId == cateId
                     select m;
        gvMobil.DataSource = mobils.ToArray();
        gvMobil.DataKeyNames = new String[] { "MobilId" };
        gvMobil.DataBind();
    }
    protected void gvMobil_SelectedIndexChanged(object sender, EventArgs e)
    {
        int mId = Convert.ToInt32(gvMobil.SelectedValue);
        var curMobil = ctx.Mobils.Where(m => m.MobilId == mId);
        dvMobil.DataSource = curMobil.ToArray();
        dvMobil.DataKeyNames = new string[] { "MobilId" };
        dvMobil.DataBind();
    }
    protected void dvMobil_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        if (e.NewMode == DetailsViewMode.Edit)
        {
            dvMobil.ChangeMode(DetailsViewMode.Edit);
            //dvMobil.DefaultMode = DetailsViewMode.Edit;
        }
        int mId = Convert.ToInt32(gvMobil.SelectedValue);
        var curMobil = ctx.Mobils.Where(m => m.MobilId == mId);
        dvMobil.DataSource = curMobil.ToArray();
        dvMobil.DataKeyNames = new string[] { "MobilId" };
        dvMobil.DataBind();
    }
}